package com.xoa.service.task.impl;


import com.xoa.dao.task.TaskManageMapper;
import com.xoa.dao.task.TaskTypeMapper;
import com.xoa.model.task.TaskManage;
import com.xoa.model.task.TaskType;
import com.xoa.model.users.Users;
import com.xoa.service.task.TaskTypeService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Service
public class TaskTypeServiceImpl implements TaskTypeService {
    @Resource
    private TaskTypeMapper taskTypeMapper;
    @Resource
    private TaskManageMapper taskManageMapper;

    @Override
    public ToJson addTaskClass(HttpServletRequest request, String className) {

        ToJson toJson= new ToJson();
        try{
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            int count = taskTypeMapper.queryTaskClass(users.getUserId());
            if(count==0){
                count =1;
            }
            TaskType taskType = new TaskType();
            taskType.setTypeName(className);
            taskType.setUserId(users.getUserId());
            taskType.setTypeNo(count);
            int i =taskTypeMapper.insertTaskClass(taskType);
            if(i>0){
                toJson.setFlag(0);
                toJson.setMsg("OK");
            }else{
                toJson.setFlag(1);
                toJson.setMsg("erro");
            }

        }catch (Exception e){
            toJson.setFlag(1);
            toJson.setMsg("erro");
            e.printStackTrace();
        }

        return toJson;
    }

    @Override
    public ToJson delTaskClass(HttpServletRequest request, int typeId) {
        ToJson toJson= new ToJson();
        try{
                int count = taskTypeMapper.delTaskClass(typeId);
                if(count>0){
                    //将删除的任务类型下的任务 任务类型编号置为空
                    List<TaskManage> taskManageList = taskManageMapper.queryTaskManageByTypeId(typeId);
                    for(TaskManage taskManag:taskManageList){
                        taskManag.setTypeId(0);
                        int i= taskManageMapper.updateTaskManage(taskManag);
                    }
                    toJson.setMsg("success");
                    toJson.setFlag(0);
                }else{
                    toJson.setMsg("erro");
                    toJson.setFlag(1);
                }
        }catch (Exception e){
            toJson.setMsg("erro");
            toJson.setFlag(1);
            e.printStackTrace();
        }

        return toJson;
    }


    @Override
    public ToJson updateTaskClass(HttpServletRequest request, TaskType taskType) {
        ToJson toJson= new ToJson();
        try{
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            taskType.setUserId(users.getUserId());
            int count = taskTypeMapper.updateTaskClass(taskType);
            if(count>0){
                toJson.setMsg("success");
                toJson.setFlag(0);
            }else{
                toJson.setMsg("erro");
                toJson.setFlag(1);
            }
        }catch (Exception e){
            toJson.setMsg("erro");
            toJson.setFlag(1);
            e.printStackTrace();
        }

        return toJson;
    }

    @Override
    public ToJson queryTaskClass(HttpServletRequest request) {
        ToJson toJson = new ToJson();
        List<TaskType> taskTypes = new ArrayList<TaskType>();
      /*  TaskType taskType = new TaskType();
        taskType.setTypeId(0);
        taskType.setTypeName("未分配");
          taskTypes.add(taskType);*/
        try{
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            List<TaskType> taskTypeList = taskTypeMapper.queryTaskTypeAll(users.getUserId());
            for(TaskType taskType1:taskTypeList){
                taskTypes.add(taskType1);
            }
            toJson.setFlag(0);
            toJson.setMsg("success");
            toJson.setObj(taskTypes);
        }catch (Exception e){
            toJson.setMsg("erro");
            toJson.setFlag(1);
            e.printStackTrace();
        }
        return toJson;
    }
}
